<template>
  <div>
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="80px">
      <el-form-item label="脚本名称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入脚本名称" />
      </el-form-item>
      <el-form-item label="使用类型" prop="usage_type">
        <el-input
          v-model="formData.usage_type"
          placeholder="请输入使用类型"
        />
      </el-form-item>
      <el-form-item label="场景标签" prop="tag">
        <el-select 
          v-model="formData.tag" 
          placeholder="请选择场景标签"
          style="width: 100%"
          value-key="id"
        >
          <el-option 
            v-for="tag in tagOptions" 
            :key="tag.name" 
            :label="tag.name" 
            :value="tag.name" 
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div class="button">
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary" @click="handleSubmit">确定</el-button>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { tagList, scriptUpdate } from '@/api/scriptLibrary'
import { ElMessage } from 'element-plus'

interface Tag {
  id: string | number
  name: string
}

interface FormData {
  id: string | number
  name: string,
  usage_type: string,
  tag: Tag
}

const emit = defineEmits<{
  update: []
  close: []
}>()

const formRef = ref<FormInstance>()
const tagOptions = ref<Tag[]>([])
const formData = reactive<FormData>({
  id: '',
  name: '',
  tag: { id: '', name: '' },
  usage_type:''
})

const props = defineProps<{
  scriptData: any
}>()

const rules: FormRules = {
  name: [
    { required: true, message: '请输入脚本名称', trigger: 'blur' }
  ],
  tag: [
    { required: true, message: '请选择场景标签', trigger: 'change' }
  ],
  usage_type: [
    { required: true, message: '请输入使用类型', trigger: 'blur' }
  ]
}

// 获取标签列表
const getTagList = async () => {
  try {
    const res = await tagList()
    console.log('[ res ] >', res)
    if (res.data.code === 200) {
      tagOptions.value = res.data.data
    }
  } catch (error) {
    console.error('获取标签列表失败:', error)
  }
}

// 初始化表单数据
const initFormData = () => {
  if (props.scriptData) {
    formData.id = props.scriptData.id
    formData.name = props.scriptData.name || ''
    formData.usage_type = props.scriptData.usage_type || ''
    // 设置单个tag对象
    if (props.scriptData.tag) {
        formData.tag = props.scriptData.tag.name
    }
  }
}

// 提交表单
const handleSubmit = async () => {
  if (!formRef.value) return
  try {
    await formRef.value.validate()
    // 准备提交数据
    const submitData = {
      id: formData.id,
      name: formData.name,
      tag: formData.tag, // 将数组转换为逗号分隔的字符串
      usage_type: formData.usage_type
    }
    // 调用更新接口
    const res = await scriptUpdate(submitData)
    if (res.data.code === 200) {
      ElMessage.success('更新成功')
      emit('update')
      emit('close')
    } else {
      ElMessage.error(res.data.msg || '更新失败')
    }
  } catch (error) {
    console.error('表单验证失败:', error)
  }
}

// 取消操作
const handleCancel = () => {
  emit('close')
}

onMounted(() => {
  console.log('[ 打印 ] >', '打印')
  getTagList()
  initFormData()
})
</script>
<style lang="scss" scoped>
.button {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}
</style>